const { $Toast } = require('../../../utils/iview/base/index');
const Tools = require('../../../utils/modules/Tools');
import NetWork from '../../../utils/modules/NetWork';

const app = getApp();

var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data: {
    province: '',
    city: '广州市',
    type: 3,
    page:1,
    banner: null,
    student: null,
    shopList: null,
    //门店展示的数据
    shopname: [],
    indexs: 0,
    vehicle: [],
    auth: false,
    actions2: [
      {
        name: '删除',
        color: '#ed3f14'
      },
    ],

    isloade: {
    hidden: false
    },
    _baseUrl: app.globalData._baseUrl
  },
  // 第一次进入页面执行
  onLoad() {
    var _that = this
    _that.setTitle('我的车辆');
   
  },



  //每次进入页面执行
  onShow: function () {
    let _that = this;
    
    var vehicle = wx.getStorageSync('vehicle');
    _that.vehicleList();
    _that.setData({
      indexs: vehicle,
      vehicle: []
    })
  },

  //点击删除显示弹窗
  actionsTap(e) {
   
    var { index, id } = e.currentTarget.dataset;
    this.setData({
      visible: true,
      index: index,
      id: id
    });
  },



  //点击修改显示弹窗
  actionsTap2(e) {

    var { index, id } = e.currentTarget.dataset;
    // this.setData({
    //   index: index,
    //   id: id 
    // });
    // 因为vehicle数组里面有多条车辆信息所有要加[index]
    wx.navigateTo({
      url: '../addVehicle/addVehicle?vehicle=' + JSON.stringify(this.data.vehicle[index]),
    })

  },


  

  //弹窗隐藏
  handleCancel2() {
    this.setData({
      visible: false,
    });

  },



  // 点击实现删除操作
  handleClickItem2() {
    let _that = this;

    var { index, id } = _that.data;
    console.log(id,"589")
    let vehicle = _that.data.vehicle;
    var token = wx.getStorageSync('token');
    const action = [...this.data.actions2];
    action[0].loading = true;

    this.setData({
      actions2: action
    });


    let net = new NetWork(_that.data._baseUrl);
    net._request({
      url: 'api/v1/member/delCar',
      data: { 
        token:token,
        car_id: id
       },
      method: 'POST'
    }, '删除', res => {
      action[0].loading = false;
      vehicle.splice(_that.data.index, 1);
      _that.setData({
        vehicle: vehicle
      });
      $Toast({
        content: '删除成功!',
        type: 'success'
      });
    }, res => {
      $Toast({
        content: '删除失败!',
        type: 'warning'
      });
    },
      res => {
        _that.setData({
          visible: false,
          actions2: action,
        });
      })

  },



  vehicleList() {
    //获取车辆列表
    var _that = this;
    var token = wx.getStorageSync('token');

    var net = new NetWork(_that.data._baseUrl);
    net._request({
      url: 'api/v1/member/mycar',
      data: {
        page: this.data.page,
        token: token
      },
      method: 'POST',
    }, '', res => {
      if (res.data.error_code == 0) {
        // 数组拼接 上一页和下一页任意滑动
        var vehicle = this.data.vehicle.concat(res.data.data.data)
        _that.setData({
          vehicle: vehicle
        })  
      }
      
      // 原本
      // if (res.data.error_code == 0) {
      //   _that.setData({
      //     vehicle: res.data.data.data
      //   })
      // }
    }, res => {
      if (res.data.error_code == 400) {
        //token过期 重新执行该函数
        app.refreshtoken(token);
        setTimeout(() => {
          _that.vehicleList();
        }, 1500)

      }
    })
  },




  addVehicle(){
    wx.navigateTo({
      url: '../addVehicle/addVehicle',
    })
    // this.vehicleList();
   
  },



  /* 拨打电话 */
  telPhone() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: ''
    }, res => {

    });
  },


  setVehicle(e) {
    var _that = this;

    var { index,id } = e.currentTarget.dataset;
    _that.setData({
      cid: id
    })
    if(!id){
      id = _that.data.cid;
    }

    var token = wx.getStorageSync('token');
    var net = new NetWork(_that.data._baseUrl);
    net._request({
      url: 'api/v1/member/car_default',
      data: { token: token, is_default: 1, car_id: id},

      method: 'POST',
    }, '设置默认', res => {
      if (res.data.error_code == 0) {

        _that.setData({
          vehicle: []
        })  

        _that.vehicleList();
      }
    }, res => {
      if (res.data.error_code == 400) {
        //token过期 重新执行该函数
        app.refreshtoken(token);
        setTimeout(() => {
          _that.setVehicle();
        }, 1500)

      }
    })
    // wx.navigateTo({
    //   url: './purchase',
    // })
  },

  onReady: function (e) {

  },


  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function () {

      }
    })
  },
  onShareAppMessage(str) {
    return {
      title: str,
      path: this.route
    }
  },


  /* 隐藏分享功能 */
  hideShare() {
    wx.hideShareMenu()
  },
  /* 分享功能 */
  showShare() {
    wx.showShareMenu()
  },


  /* 下拉刷新触发功能 */
  onPullDownRefresh() {
    // 下拉刷新事件
    // wx.startPullDownRefresh
    // wx.stopPullDownRefresh
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  

  },




  /* 开启上拉到底部触发功能 */
  startDonwEven() {
    wx.startPullDownRefresh()
  },
  /* 停止上拉到底部触发功能 */
  stopDownEven() {
    wx.stopPullDownRefresh()
  },



  onReachBottom() {
    console.log("onomom")
   
      var page = this.data.page;
      this.setData({
        page: page + 1
      })
    this.vehicleList();
  
    // 上拉触底事件
    //console.log('触发触底事件')
  },



  onPageScroll(res) {
    // 页面滚动事件
    //console.log(res.scrollTop)
  },

  userInfo(e) {
    let _userInfo = e.detail.userInfo;
    let net = new NetWork(app.globalData._baseUrl);

    if (_userInfo) {
      this.setData({
        auth: true
      })
    }

    wx.login({
      success(res) {
        //console.log('小程序登录信息', res)
        if (res.code) {
          net._request({
            url: 'login',
            data: {
              code: res.code
            },
          }, false, sres => {

          });
        }
      }
    })
  }


})
